<template>
  <template v-if="user">
    <van-cell title="当前用户" :value="user?.username" />
    <van-cell title="修改信息" is-link to="/user/update" />
    <van-cell title="我创建的队伍" is-link to="/user/team/create" />
    <van-cell title="我加入的队伍" is-link to="/user/team/join" />
    <van-cell title="退出" @click="logout" />
  </template>
</template>

<script setup lang="ts">
import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import myAxios from "../plugins/myAxios";
import {Toast} from "vant";
import {getCurrentUser} from "../services/user";


const user = ref();

const router = useRouter();

onMounted(async () => {
  user.value = await getCurrentUser();
})

const toEdit = (editKey: string, editName: string, currentValue: string) => {
  router.push({
    path: '/user/edit',
    query: {
      editKey,
      editName,
      currentValue,
    }
  })
}

const logout = async () => {
  try {
    // 调用后端接口执行退出操作
    await myAxios.post('/user/logout');
    // 清除本地用户信息
    user.value = null;
    // 跳转到登录页或其他适当的页面
    router.push('/login');
    Toast.success('成功退出');
  } catch (error) {
    Toast.fail('退出失败，请重试');
    console.error('退出失败：', error);
  }
}

</script>

<style scoped>

</style>
